﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace dvd_xml
{
    public partial class EdytujFilm : Form
    {
        private Film f;
        public EdytujFilm(Film f)
        {
            InitializeComponent();
            this.f = f;
        }

        private void EdytujFilm2_Load(object sender, EventArgs e)
        {
            textBoxAddDirector.Text = f.Rezyser;
            textBoxAddLength.Text = Convert.ToString(f.Dlugosc);
            textBoxAddTitle.Text = f.Tytul;
            textBoxAddYear.Text = Convert.ToString(f.Rok);

            StringBuilder aktorzy = new StringBuilder();
            foreach (string a in f.Aktorzy)
            {
                aktorzy.Append(a);
                aktorzy.Append(',');
            }
            aktorzy.Remove(aktorzy.Length - 1, 1);

            textBoxAddActors.Text = aktorzy.ToString();
        }

        private void btnAnuluj_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnZapisz_Click(object sender, EventArgs e)
        {
            XDocument xmlDoc = XDocument.Load(Form1.PATH + "filmy.xml");
            XElement filmy = xmlDoc.Descendants("Film").
                Where(fi => fi.Element("Tytul").Value == f.Tytul).First();

            filmy.Element("Tytul").Value = textBoxAddTitle.Text;
            filmy.Element("Rezyser").Value = textBoxAddDirector.Text;
            filmy.Element("Rok").Value = textBoxAddYear.Text;
            filmy.Element("Dlugosc").Value = textBoxAddLength.Text;

            String[] actor = textBoxAddActors.Text.Split(',');
            XElement[] element = new XElement[actor.Length];
            for (int i = 0; i < element.Length; i++)
            {
                element[i] = new XElement("Aktor", actor[i]);
            }
            filmy.Element("Aktorzy").RemoveNodes();
            filmy.Element("Aktorzy").Add(element);

            xmlDoc.Save(Form1.PATH + "filmy.xml");

            Close();
        }
    }
}
